package com.mec.web.y2012.ajax;

import java.io.IOException;
import java.io.PrintWriter;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.apache.log4j.Logger;

/**
 * Servlet implementation class DynamicUpdateServlet
 */
@WebServlet("/ajax/dynamicUpdate")
public class DynamicUpdateServlet extends HttpServlet {
	private static final long serialVersionUID = 1L;
    private int counter = 1;
    
    public DynamicUpdateServlet() {
        super();
    }

	protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		String res = "";
		String task = request.getParameter("task");
		String message = "";
		
		if("reset".equals(task)){
			counter = 1;
		}else{
			switch(counter){
			case 1: message = "Steve walks on stage"; break;
			case 2: message = "iPods rock"; break;
			case 3: message = "Steve says Macs rule"; break;
			case 4: message = "Change is coming"; break;
			case 5: message = "Yes, OS X runs on Intel - has for years."; break;
			case 6: message = "Macs will soon have Intel chips"; break;
			case 7: message = "done"; break;
			}
			
			++counter;
		}

		//
		res="<message>" +message +"</message>";
		
		//
		PrintWriter out = response.getWriter();
		response.setContentType("text/xml");
		response.setHeader("Cache-Control", "no-cache");
		
		out.println("<response>");
		out.println(res);
		out.println("</response>");
		out.close();
	}

	protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		doGet(request, response);
	}
	
	
	private static final Logger logger = 
			Logger.getLogger(DynamicUpdateServlet.class);

}
